package org.homeorganiser.flex.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.homeorganiser.flex.dto.Publication;
import org.homeorganiser.flex.util.Constant;

public class PublicationDAO implements IGenericDAO<Publication> {

	private final DataSource dataSource;
	Connection conn = null;
	ResultSet rs = null;
	PreparedStatement ps;

	public PublicationDAO(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	public List<Publication> findAll(int type) {
		List<Publication> publicationList = new ArrayList<Publication>();

		Publication pub;
		try {

			conn = this.dataSource.getConnection();
			ps = conn.prepareStatement(Constant.SQL_SELECT_ALL_PUBLICATION);
			ps.setInt(1, type);
			rs = ps.executeQuery();

			while (rs.next()) {
				pub = new Publication();
				pub.setId(rs.getInt("id"));
				pub.setAddedBy(rs.getString("addedBy"));
				pub.setPublicationName(rs.getString("publicationname"));
				publicationList.add(pub);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
		return publicationList;
	}

	public List<Publication> findByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	public Publication findById(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	public Publication create(Publication pub) {
		try {
			conn = this.dataSource.getConnection();
			ps = conn.prepareStatement(Constant.SQL_INSERT_PUBLICATION_TABLE,
					new String[] { "ID" });

			ps.setString(1, pub.getPublicationName());
			ps.setInt(2, pub.getInventorytype());
			ps.setString(3, pub.getAddedBy());

			ps.executeUpdate();
			ResultSet rs = ps.getGeneratedKeys();
			rs.next();
			// Update the id in the returned object. This is important as this
			// value must be returned to the client.
			int id = rs.getInt(1);
			pub.setId(id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
		return pub;
	}

	public boolean update(Publication item) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean remove(Publication item) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean removeAll(int type) {
		try {

			conn = this.dataSource.getConnection();
			ps = conn.prepareStatement(Constant.SQL_DELETE_ALL_PUBLICATION);
			ps.setInt(1, type);
			return ps.executeUpdate() == 1;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
	}

	
}
